package org.snaggywolf.clipboardcall;

import org.snaggywolf.clipboardcall.R;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

public class ClipboardCall extends Activity {
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        OnClickListener listener = new View.OnClickListener() {
            public void onClick(View v) {
        		switch (v.getId()) {
	    			case R.id.close_button:
	    				finish();
	    				break;
	    			case R.id.ok_button:
	    				makeCall();
	    				//finish();
	    				break;		
	    			case R.id.sms_button:
	    				sendSms();
	    				//finish();
	    				break;
        		}
            }
        };
        
        // find required controls
    	EditText phoneText = (EditText)findViewById(R.id.phone_number);  
    	TextView clipText = (TextView)findViewById(R.id.clipboard_content);  
		View closeButton = findViewById(R.id.close_button);
		View okButton = findViewById(R.id.ok_button);
		View smsButton = findViewById(R.id.sms_button);

		// set listeners
		closeButton.setOnClickListener(listener);	
		okButton.setOnClickListener(listener);
		smsButton.setOnClickListener(listener);
		
		// watcher will change only one button so we will pass in constructor only this button
		phoneText.addTextChangedListener(new Watcher(okButton, smsButton));
		
		// parse clip board text
        ClipboardManager buffer = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
        String phoneNumber = buffer.hasText() ? buffer.getText().toString() : "";
    	phoneNumber = phoneNumber.replaceAll("[^\\+0-9]", "");
    	phoneNumber = phoneNumber.startsWith("+") ? "+" + phoneNumber.replaceAll("[\\D]", "") : phoneNumber.replaceAll("[\\D]", "");
    	
    	if (phoneNumber.equals(""))
        {
        	okButton.setEnabled(false);
        	smsButton.setEnabled(false);
        	clipText.setText(R.string.error_label);
        	phoneText.setEnabled(false);
        } else {
        	smsButton.setEnabled(true);
        	okButton.setEnabled(true);
        	phoneText.setEnabled(true);
        	clipText.setText(buffer.getText().toString());
        	phoneText.setText(phoneNumber);
        }

    }
        
    /** Send SMS */
    protected void sendSms() {
        try {
	    	String phoneNumber = GetPhoneNumber();
	    	if (phoneNumber == null || phoneNumber.equals("")) 
	    		return;
	    	
	    	// try to send sms
	        Intent smsIntent = new Intent(Intent.ACTION_VIEW);
	        //smsIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
	        smsIntent.putExtra("address", phoneNumber);
	        smsIntent.setType("vnd.android-dir/mms-sms");
	        startActivity(smsIntent);
        } catch (ActivityNotFoundException activityException) {
            Log.e("SMS application", "SMS failed", activityException);
       }	        
	}

	/** Make a phone call */
    private void makeCall() {
        try {
        	String phoneNumber = GetPhoneNumber();
        	if (phoneNumber == null || phoneNumber.equals("")) 
        		return;
        	
        	// try to make call
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:" + phoneNumber));
            //callIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            startActivity(callIntent);
        } catch (ActivityNotFoundException activityException) {
             Log.e("Dialing application", "Call failed", activityException);
        }
    }
    
    /** Get phone number from edittext view */
    private String GetPhoneNumber() {
    	// check phone number
    	EditText phoneText = (EditText)findViewById(R.id.phone_number);  
    	String phoneNumber = phoneText.getText().toString();
		return phoneNumber;    	
    }
}

/** Text watcher to check content of input box */
class Watcher implements TextWatcher {

	private View okButton;
	private View smsButton;
	
	public Watcher(View OkButton, View SmsButton) {
		okButton = OkButton;
		smsButton = SmsButton;
	}

	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub
		
	}

	public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		// TODO Auto-generated method stub
		
	}

	public void onTextChanged(CharSequence s, int start, int before, int count) {
		if (s.length() < 1) {
			okButton.setEnabled(false);		
			smsButton.setEnabled(false);
		}
		else {
			okButton.setEnabled(true);	
			smsButton.setEnabled(true);
		}
	}
	
}